<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>给角色授权</title>
    <link rel="stylesheet" type="text/css" href="../css/zTreeStyle.css" >
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
    <script type="text/javascript" src="../lib/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../lib/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="../lib/jquery.ztree.excheck-3.5.js"></script>
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
</head>
<script type="text/javascript">

    var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
    var role_id = "";
    if(_urlparams && _urlparams._id){
        role_id =  _urlparams._id;
    }

    //关闭窗口
    function doClose() {
        parent.closeWin();
    }

    //保存
    function doSave() {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        var auths = "";
        if(nodes!=null&&nodes.length>0) {
            for(var i=0;i<nodes.length;i++){
                var res_id = nodes[i].id;
                auths += '{res_id:"'+res_id+'"}#';
            }
        }
        if(auths!="") {
            auths = auths.substring(0,auths.length-1);
        }else {
            auths = "0";
        }
        $.ajax({
            type:"POST",
            url:'/role/auth/save',
            data:{'auths':auths,'role_id':role_id},
            success:function(data){
                if("success"==data.flag) {
                   alert('业务处理成功!')
                   parent.closeWin();
                }else {
                    alert('业务处理失败！'+data.msg);
                }
            },
            error:function(e){
                alert('业务处理失败！');
            }

        });
    }

    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    var zNodes =[
        { id:'0', pId:'', name:"资源根节点", open:true}
    ];

    var code;

    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
                type = { "Y" : "ps", "N" : "s" };
        zTree.setting.check.chkboxType = type
        showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
    }
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>"+str+"</li>");
    }

    $(document).ready(function(){

        $.ajax({
            type: "GET",
            url: "/role/auth",
            data: {role_id:role_id},
            async: true,
            dataType:"json",
            success: function(data) {
                $.each(data.znodes,function(i,node){
                    zNodes.push(node);
                });

                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                setCheck();
                $("#py").bind("change", setCheck);
                $("#sy").bind("change", setCheck);
                $("#pn").bind("change", setCheck);
                $("#sn").bind("change", setCheck);
            },
            error: function(e) {
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                setCheck();
                $("#py").bind("change", setCheck);
                $("#sy").bind("change", setCheck);
                $("#pn").bind("change", setCheck);
                $("#sn").bind("change", setCheck);
            }
        });

    });
</script>
<body>

<div style="margin-left: 30px;margin-top: 10px">
    <div class="row">
        <div class="col-sm-2 col-sm-offset-4">
            <button type="button" class="btn btn-default" onclick="doSave()">保存</button>
            <button type="button" class="btn btn-default" onclick="doClose()">关闭</button>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-3">
            <ul id="treeDemo" class="ztree"></ul>
            <input type="hidden" name="res_ids" id="res_ids"/>
        </div>
    </div>
</div>

</body>
</html>